package com.lj.page;

import com.lj.dao.ParamsInfoDao;
import com.lj.dao.SqliteHelper;
import com.lj.domain.ParamsInfo;
import com.lj.domain.SimpleResponse;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by dave_suwei on 2017/12/7.
 */
public class ParamsGetServlet extends HttpServlet{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding( "UTF-8" );

        SimpleResponse sr=new SimpleResponse();
        sr.setOk(true);
        sr.setMsg("成功!");
        //取参数
        ParamsInfo paramsInfo=null;
        SqliteHelper sh= (SqliteHelper) req.getAttribute("sh");
        if(sh!=null) {
            ParamsInfoDao paramsInfoDao = new ParamsInfoDao(sh);
            paramsInfo=paramsInfoDao.getParams();
        }
        sr.setEntry(paramsInfo);

        if(paramsInfo==null){
            sr.setOk(false);
            sr.setMsg("!");
        }

        sr.setEntry(paramsInfo);

        System.out.println("paramsInfo->"+paramsInfo);

        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().println(sr.toJson());
    }
}
